package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.mvp.contract.FindPwdContract;
import com.turing.usdtpay.mvp.presenter.FindPwdPresenter;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/9 15:29
 * @描述：
 **/
public class RetrievePwdActivity extends Activity implements View.OnClickListener ,FindPwdContract.View {
    private BaseTitleView mTitleView;
    private LinearLayout mLayoutVerify,mLayoutSubmit;
    private EditText mAccount,mVerify,mPwd,mConfirmPwd;
    private Button mNext,mSubmit;
    private TextView mSend;
    private boolean isNext = false;
    private FindPwdContract.Present mPresent;
    private int time = 120;
    private boolean isCount = false;
    private String mEmail,mCode;
    private CountDownTimer countDownTimer = new CountDownTimer(120000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            isCount = true;
            time = -- time ;
            mSend.setText(time+"秒");

        }

        @Override
        public void onFinish() {
            time = 120 ;
            isCount = false;
            mSend.setText("发送");
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_pwd);
        initView();
        setPresenter(new FindPwdPresenter(this));
    }

    private void initView() {
        mLayoutVerify = (LinearLayout) findViewById(R.id.verify_layout);
        mLayoutSubmit = (LinearLayout) findViewById(R.id.submit_layout);
        mAccount = (EditText) findViewById(R.id.et_account);
        mVerify = (EditText) findViewById(R.id.et_verify);
        mPwd = (EditText) findViewById(R.id.et_pwd);
        mConfirmPwd = (EditText) findViewById(R.id.et_confirm_pwd);
        mSend = (TextView) findViewById(R.id.tv_verify);
        mNext = (Button) findViewById(R.id.btn_next);
        mSubmit = (Button) findViewById(R.id.btn_submit);
        mTitleView = (BaseTitleView) findViewById(R.id.title);
        mTitleView.setLeftShow(true);
        mTitleView.setLogoShow(false);
        mTitleView.setTitle("找回密码");
        mTitleView.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSend.setOnClickListener(this);
        mNext.setOnClickListener(this);
        mSubmit.setOnClickListener(this);
        mLayoutVerify.setVisibility(View.VISIBLE);
        mLayoutSubmit.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mSend.getId()){
            if (!FormatUtils.isEmail(mAccount.getText().toString())){
                ToastUtils.showToast(this,"请输入有效邮箱地址");
                return;
            }
            if (!isCount){
                countDownTimer.start();
                mPresent.sendCode(mAccount.getText().toString(),2);
            }else {
                ToastUtils.showToast(this,"请稍后再试");
            }
        }else if (v.getId() == mNext.getId()){
            if (!FormatUtils.isEmail(mAccount.getText().toString())){
                ToastUtils.showToast(this,"请输入有效邮箱地址");
                return;
            }

            if (TextUtils.isEmpty(mVerify.getText().toString())){
                ToastUtils.showToast(this,"验证码不能为空");
                return;
            }
            mEmail = mAccount.getText().toString();
            mCode = mVerify.getText().toString();
            mLayoutVerify.setVisibility(View.GONE);
            mLayoutSubmit.setVisibility(View.VISIBLE);
        }else if (v.getId() == mSubmit.getId()){
            if (!FormatUtils.checkPasswordRule(mPwd.getText().toString())||!FormatUtils.checkPasswordRule(mConfirmPwd.getText().toString())){
                ToastUtils.showNormolToast(this,"密码格式为6-16位字符，需同时包含大小写字母和数字，可包含特殊字符");
                return ;
            }

            if (!mPwd.getText().toString().equals(mConfirmPwd.getText().toString())){
                ToastUtils.showToast(this,"两个密码不一致");
                return;
            }
            mPresent.resetPwd(mEmail,mPwd.getText().toString(),mCode);

        }
    }


    @Override
    public void sendSuccess() {
        ToastUtils.showToast(this,"发送成功");
    }

    @Override
    public void resetSuccess() {
        ToastUtils.showNormolToast(this,"修改成功");
        finish();
    }


    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(this,msg);
    }

    @Override
    public void setPresenter(FindPwdContract.Present presenter) {
        mPresent = presenter;
    }

    @Override
    protected void onDestroy() {
        if (countDownTimer != null){
            countDownTimer.cancel();
            countDownTimer = null;
        }
        super.onDestroy();
    }
}
